<?php

class PagesController extends Controller
{
    public $breadcrumbs;
	public $key = 'fucking code';
	public function actions()
	{
		return array(
			// captcha action renders the CAPTCHA image displayed on the contact page
			'captcha'=>array(
				'class'=>'CCaptchaAction',
				'backColor'=>0xFFFFFF,
			),
			// page action renders "static" pages stored under 'protected/views/site/pages'
			// They can be accessed via: index.php?r=site/page&view=FileName
			'page'=>array(
				'class'=>'CViewAction',
			),
		);
	}
    public function actionAbout()
    {
        $about = Advertising::model()->find('type=:type', array(':type' => Advertising::AD_TYPE_ABOUT ));
        if($about === null)
            throw new CHttpException(404, yii::t("error", "The content is updated"));

        $this->breadcrumbs = array( array('name' => Yii::t("view_partials_header","About"), 'url' => $this->createUrl('pages/about')));
        $this->layout = "PageLayout";
        $this->render("About", array('about' => $about));
    }

    public function actionContact()
    {
		$list_mail = array('lekimthuy@boxanhfoods.vn','lequoccuong@boxanhfoods.vn','ngongocgiang@boxanhfoods.vn','nguyenkyquang@boxanhfoods.vn');
        $contact = Advertising::model()->find('type=:type', array(':type' => Advertising::AD_TYPE_CONTACT));
        if($contact === null)
            throw new CHttpException(404, yii::t("error", "The content is updated"));
		
		$model=new ContactForm;
		if(isset($_POST['ContactForm']))
		{
			$model->attributes=$_POST['ContactForm'];
			if($model->validate())
			{
				$str = "Chào bạn,<br><br>Thông tin khách hàng liên hệ:<br><br>";
				$str .= "Họ tên: ".$model->name."<br>
						 Email: ".$model->email."<br>
						 SĐT: ".$model->phone."<br>
						 Nội dung: ".$model->body;
							
				$mail2 = Yii::createComponent('application.extensions.mailer.EMailer');							
				$mail2->Host = 'mail.boxanhfoods.vn';							
				$mail2->IsSMTP();							
				$mail2->IsHTML(true);							
				$mail2->CharSet = 'UTF-8';							
				$mail2->SMTPAuth=true;							
				$mail2->From = 'notification@boxanhfoods.vn';							
				$mail2->Port = 25;							
				$mail2->Username = 'notification@boxanhfoods.vn';							
				$mail2->Password = 'R#[rl=#0$q0M';							
				$mail2->FromName = 'BoXanhFood liên hệ';							
				$mail2->AddReplyTo('notification@boxanhfoods.vn');		
				foreach($list_mail as $value)
					$mail2->AddAddress($value);							
				$mail2->Subject = '[BoXanhFood] Khách hàng liên hệ';							
				$mail2->Body =  $str.'<br><br> Trân trọng,<br>Cám ơn.';							
				$mail2->Send();
				
				// Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.');
				$time = time();
				
				$this->redirect($this->createUrl('pages/submitContactSuccess',array('time'=>$time,'token'=>md5($time.$this->key))));
			}
		}
        $this->breadcrumbs = array( array('name' => Yii::t("view_partials_header","Contact"), 'url' => $this->createUrl('pages/contact')));
        $this->layout = "PageLayout";
        $this->render("Contact", array('contact' => $contact,'model'=>$model));
    }

    public function actionSubmitContactSuccess($time='',$token='')
    {
		if($time && $token){
			$info['time'] = $time;
			$info['token'] = $token;
			
			$this->layout = "PageLayout";
			$this->render("submit-contact-success",array(
						'info'		=> $info,
				));
		}
		else{
			$info['order_id'] = 0;
			$info['time'] = 0;
			$info['token'] = 0;
			
			$this->layout = "PageLayout";
			$this->render("submit-contact-success",array(
						'info'		=> $info,
				));
		}
    }
    public function actionServices()
    {
        $this->breadcrumbs = array( array('name' => Yii::t("view_partials_header","Services"), 'url' => $this->createUrl('pages/services')));
        $this->layout = "PageLayout";
        $this->render("Services");
    }
}